//
//  MainViewController.swift
//  weibo
//
//  Created by wang_h on 2017/5/24.
//  Copyright © 2017年 wang_h. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    lazy var imageNames = ["tabbar_home","tabbar_message_center","","tabbar_discover","tabbar_profile"]
//    lazy var composeBtn : UIButton = UIButton.createButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")
    lazy var composeBtn : UIButton = UIButton(imageName: "tabbar_compose_icon_add", bgImageName: "tabbar_compose_button")

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupComposeBtn()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setupTabBarItem()
    }
}

//MARK:- 设置UI界面
extension MainViewController {
    /// 设置发布按钮
    func setupComposeBtn() {
        //1.将composeBtn添加到tabBar中
        tabBar.addSubview(composeBtn)
        //2.设置位置
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
        //3.监听发布按钮的点击
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
    }
    ///设置tabBarItem
    func setupTabBarItem(){
        //1.遍历所有的item
        let itemsCount = tabBar.items?.count
        
        for i in 0 ..< itemsCount! {
            //2.获取item
            let item = tabBar.items![i]
            //3.如果下标值为2，则该item不可以和用户交互
            if i == 2 {
                item.isEnabled = false
                continue
            }
            //4.设置其它item的选中时候得图片
            item.selectedImage = UIImage(named: imageNames[i] + "_highlighted")
        }
    }
}
//MARK:- 事件监听
extension MainViewController {
    //事件监听本质发送消息，但是发送消息是OC的特性
    //将方法包装成@SEL --> 类中查找方法列表 --> 根据@SEL找到imp指针（函数指针） -->执行函数
    //如果swift中将一个函数声明称private，那么该函数不会被添加到方法列表中
    func composeBtnClick() {
        print("composeBtnClick")
    }
}
